//
//  AddSizeViewController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/22.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "InOutAccountViewController.h"

@interface InOutAccountViewController ()
{
    NSMutableArray *buttonList;
    PMCalendarController *pmCC;
    NSDate *nowdate;
}
@end

@implementation InOutAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initUI
{
    UIImage *image = _bgImage.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    _bgImage.image=image;
    self.view.backgroundColor=[UIColor clearColor];
    
    _noteField.layer.borderColor=[UIColor colorWithWhite:0.85 alpha:1].CGColor;
    _noteField.layer.borderWidth=0.5;
    _noteField.layer.cornerRadius=5;
    _noteField.clipsToBounds=YES;
    
    
    _dateField.delegate=self;
    _dateField.tag=1;
    _dateField.inputView=[[UIView alloc] init];
    _dateField.inputAccessoryView=[[UIView alloc] init];
    _dateField.text=[nowdate dateStringWithFormat:YYYYMMDD];
    
    [self addTextFieldChange:_pay1Field];
    [self addTextFieldChange:_pay2Field];
    [self addTextFieldChange:_pay3Field];
    [self addTextFieldChange:_pay4Field];
    [self addTextFieldChange:_pay5Field];

    
    [_submitBtn bk_whenTapped:^{
        
        
        if([_pay1Field.text isBlank] && [_pay2Field.text isBlank]  && [_pay3Field.text isBlank] && [_pay4Field.text isBlank] && [_pay5Field.text isBlank])
        {
            [self showAlert:@"至少填写一样"];
            return ;
        }
        
        
        if(_accountType==AccountTypeSupplier)
        {
            
            PayOutModel *model=[[PayOutModel alloc] init];
            model.payDate=[nowdate dateStringWithFormat:YYYYMMDDHHMMSS];
            model.remark=_noteField.text;
            model.supplierId=_supplier.supplierId;
            model.amount=[_totalField.text doubleValue];
            model.amountPayType1=[_pay1Field.text doubleValue];
            model.amountPayType2=[_pay2Field.text doubleValue];
            model.amountPayType3=[_pay3Field.text doubleValue];
            model.amountPayType4=[_pay4Field.text doubleValue];
            model.amountPayType5=[_pay5Field.text doubleValue];
            
            model.amount=model.amountPayType1+model.amountPayType2+model.amountPayType3+model.amountPayType4+model.amountPayType5;
            
            if(_payout)
            {
                model.payOutId=_payout.payOutId;
               [model updateToDB];
            }
            else [PayOutModel insertToDB:model];
        }
        else
        {
            PayInModel *model=[[PayInModel alloc] init];
            model.payDate=[nowdate dateStringWithFormat:YYYYMMDDHHMMSS];
            model.remark=_noteField.text;
            model.clientId=_client.clientId;
            model.amountPayType1=[_pay1Field.text doubleValue];
            model.amountPayType2=[_pay2Field.text doubleValue];
            model.amountPayType3=[_pay3Field.text doubleValue];
            model.amountPayType4=[_pay4Field.text doubleValue];
            model.amountPayType5=[_pay5Field.text doubleValue];
            
            model.amount=model.amountPayType1+model.amountPayType2+model.amountPayType3+model.amountPayType4+model.amountPayType5;
            
            if(_payin)
            {
                model.payInId=_payin.payInId;
                [model updateToDB];
            }
            [PayInModel insertToDB:model];
        }
        
        
        if([_delegate respondsToSelector:@selector(didChange:andData:)])
        {
            [_delegate didChange:ChangeTypeDefult andData:nil];
        }

        
        
        [self showAlert:@"添加成功" handler:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
    
    
    [_closeBtn bk_whenTapped:^{
           [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    
  

}


-(void) initData
{
    buttonList=[[NSMutableArray alloc] init];
    nowdate=[NSDate date];
    
    NSArray *paylist=[[CoreArchive strForKey:NSDEFAULT_ST_PAYMENT] componentsSeparatedByString:@"|"];
    _pay1Label.text=[paylist objectAtIndex:0];
    _pay2Label.text=[paylist objectAtIndex:1];
    _pay3Label.text=[paylist objectAtIndex:2];
    _pay4Label.text=[paylist objectAtIndex:3];
    _pay5Label.text=[paylist objectAtIndex:4];
    
    
    switch (_accountType) {
        case AccountTypeSupplier:
            _titleLable.text=@"付款供应商";
            
            if(_payout)
            {
                
                nowdate=[NSDate dateFromString:_payout.payDate andFormat:YYYYMMDDHHMMSS];
                _noteField.text=_payout.remark;
                _totalField.text=[ToolUtils priceToStr:_payout.amount];
                _pay1Field.text=(_payout.amountPayType1>0?[ToolUtils priceToStr:_payout.amountPayType1]:@"");
                _pay2Field.text=(_payout.amountPayType2>0?[ToolUtils priceToStr:_payout.amountPayType2]:@"");
                _pay3Field.text=(_payout.amountPayType3>0?[ToolUtils priceToStr:_payout.amountPayType3]:@"");
                _pay4Field.text=(_payout.amountPayType4>0?[ToolUtils priceToStr:_payout.amountPayType4]:@"");
                _pay5Field.text=(_payout.amountPayType5>0?[ToolUtils priceToStr:_payout.amountPayType5]:@"");
                
            }
           
            
            break;
        case AccountTypeClient:
        case AccountTypeAllocate:
            _titleLable.text=@"客户到账";
            
            
            if(_payin)
            {
                
                nowdate=[NSDate dateFromString:_payin.payDate andFormat:YYYYMMDDHHMMSS];
                _noteField.text=_payin.remark;
                _totalField.text=[ToolUtils priceToStr:_payin.amount];
                _pay1Field.text=(_payin.amountPayType1>0?[ToolUtils priceToStr:_payin.amountPayType1]:@"");
                _pay2Field.text=(_payin.amountPayType2>0?[ToolUtils priceToStr:_payin.amountPayType2]:@"");
                _pay3Field.text=(_payin.amountPayType3>0?[ToolUtils priceToStr:_payin.amountPayType3]:@"");
                _pay4Field.text=(_payin.amountPayType4>0?[ToolUtils priceToStr:_payin.amountPayType4]:@"");
                _pay5Field.text=(_payin.amountPayType5>0?[ToolUtils priceToStr:_payin.amountPayType5]:@"");
                
            }
            
           
            
            break;
            
        default:
            break;
    }
    
    
    [self reloadData];

    
}


-(void)textFieldChanged:(id)sender
{
    _totalField.text=[ToolUtils priceToStr:[_pay1Field.text doubleValue]+[_pay2Field.text doubleValue]+[_pay3Field.text doubleValue]+[_pay4Field.text doubleValue]+[_pay5Field.text doubleValue]];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag==1 && [pmCC isCalendarVisible])
    {
        [pmCC dismissCalendarAnimated:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField.tag==1)
    {
        pmCC = [[PMCalendarController alloc] initWithThemeName:@"default"];
        pmCC.delegate = self;
        pmCC.mondayFirstDayOfWeek = NO;
        if ([pmCC isCalendarVisible])
        {
            [pmCC dismissCalendarAnimated:NO];
        }
         pmCC.period = [PMPeriod oneDayPeriodWithDate:nowdate];
        [pmCC presentCalendarFromView:_dateField permittedArrowDirections:PMCalendarArrowDirectionUp animated:YES];
    }
}

#pragma PMCalendarControllerDelegate
- (void)calendarControllerDidDismissCalendar:(PMCalendarController *)calendarController
{
    [_dateField resignFirstResponder];
}

- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod;
{
    nowdate=newPeriod.startDate;
    _dateField.text=[NSString stringWithFormat:@"%@",[newPeriod.startDate dateStringWithFormat:YYYYMMDD]];
    [self reloadData];
}


-(void) reloadData
{
    if(_accountType==AccountTypeSupplier)
        _totalAmountField.text=[NSString stringWithFormat:@"%0.2f",[AppService getAmountByUserIdAndDateAndType:_supplier.supplierId andDate:[nowdate dateStringWithFormat:YYYYMMDDHHMMSS] andType:_accountType]];
    else  _totalAmountField.text=[NSString stringWithFormat:@"%0.2f",[AppService getAmountByUserIdAndDateAndType:_client.clientId andDate:[nowdate dateStringWithFormat:YYYYMMDDHHMMSS] andType:_accountType]];
}


@end
